import axios from 'axios'

import store from '@/store/index'

import address from '@/filters/address'

axios.defaults.timeout = 1000 * 6;  // 超时时间为6s

import {Message} from "element-ui";

let domain = process.env.VUE_APP_URL

// 请求前拦截
axios.interceptors.request.use(
 config => {
  if (store.getters.getToken !== '') {
   config.headers['acce-token'] = store.getters.getToken
  }

  if (store.getters.getLang === 'ens') {
   config.headers['language'] = 'ENS'
  }

  if (store.getters.getLang === 'rus') {
   config.headers['language'] = 'RUS'
  }

  return config
 }
)

// 请求成功后对返回参数进行判断
axios.interceptors.response.use(
 response => {

  return response.data
 },
 error => {

 }
)

console.log(address.login)

// 登录
export function LOGIN(params) {
 return new Promise((resolve, reject) => {
  axios.post(domain + address.login, params)
   .then(res => {
    if (res.code !== 1) {
     Message.error(res.msg)
    }
    resolve(res)
   })
   .catch(err => {
    console.log(err)
   })
 })
}

// 注册
export function REGISTER(params) {
 return new Promise((resolve, reject) => {
  axios.post(domain + address.register, params)
   .then(res => {
    if (res.code !== 1) {
     Message.error(res.msg)
    }
    resolve(res)
   })
   .catch(err => {
    console.log(err)
   })
 })
}

// 发送验证码
export function SEND(params) {
 return new Promise((resolve, reject) => {
  axios.post(domain + address.send, params)
   .then(res => {
    if (res.code === 1) {
     Message.success(this.$t('common.success.t8'));
    } else {
     Message.error(res.msg)
    }
    resolve(res)
   })
   .catch(err => {
    console.log(err)
   })
 })
}

// POST请求
export function POST(api, params) {
 const apiHost = domain + api;
 return new Promise((resolve, reject) => {
  axios.post(apiHost, params)
   .then(res => {
    if (res.code !== 1) {
     Message.error(res.msg)
    }
    resolve(res)
   })
   .catch(err => {

   })
 })
}

// GET请求
export function GET(api, params) {
 const apiHost = domain + api;
 return new Promise((resolve, reject) => {
  axios.get(apiHost, {params})
   .then(res => {
    if (res.code !== 1) {
     Message.error(res.msg)
    }
    resolve(res)
   })
   .catch(err => {

   })
 })
}
